﻿using UnityEngine;
using System.Collections;

public class Enemy1 : MonoBehaviour {

    //移动速度
    public float enemy_Speed = 1;
    //敌人的生命
     int enemy_Life = 1;
    //是否移动
    protected bool yidong_Bool = true;
    Transform m_Transform;
	void Start () {

        m_Transform = this.transform;
	}
	void Update () {

        UpdateMove();
	}

    protected void UpdateMove()
    {
        if (yidong_Bool)
        {
            //m_Transform.Translate(0, enemy_Speed * Time.deltaTime, 0);
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag.CompareTo("PlayerRocket1") == 0)
        {
            Rocket1 rocket = other.GetComponent<Rocket1>();
            if (rocket != null)
            {
                enemy_Life -= rocket.rocket_Power;
				Destroy(rocket.gameObject);
                if (enemy_Life <= 0)
                {
                    this.teXiao();

                }
            }
		}else if(other.tag.CompareTo("PlayerRocket2") == 0)
		{
			Rocket2 rocket2 = other.GetComponent<Rocket2>();
			if(rocket2!=null)
			{
				enemy_Life -= rocket2.rocket_Power;
				Destroy(rocket2.gameObject);
				if (enemy_Life <= 0)
				{
					this.teXiao();
					
				}
			}

		}else if(other.tag.CompareTo("PlayerRocket3") == 0)
		{
			Rocket3 rocket3 = other.GetComponent<Rocket3>();
			if(rocket3!=null)
			{
				enemy_Life -= rocket3.rocket_power;
				Destroy(rocket3.gameObject);
				if (enemy_Life <= 0)
				{
					this.teXiao();
					
				}
			}
		}else if(other.tag.CompareTo("HeDan") ==0 )
		{
			this.teXiao();
		}
        else if (other.tag.CompareTo("Player") == 0)
        {
            this.teXiao();
        }else if(other.tag.CompareTo("Bounds") == 0)
		{
			Destroy(this.gameObject);
		}
    }

    //爆炸并且播放音效
    protected void teXiao()
    {
        yidong_Bool = false;
		Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[11].ToString()), m_Transform.position, Quaternion.identity);
		Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[19].ToString()),m_Transform.position,Quaternion.identity);
		//出现钻石
		int number =0;
		number = Random.Range(3,6);
		GameObject obj_ZuanShi = Resources.Load<GameObject>(SampleXml.prefabPath[5].ToString());
		for(int i=0;i<number;i++)
		{
			Instantiate(obj_ZuanShi,this.ZuanShiPosition(m_Transform.position),Quaternion.identity);
		}
        Destroy(this.gameObject);     
    }
	//金币移动出现
	protected Vector3 ZuanShiPosition(Vector3 vec)
	{
		Vector3 vec3 = new Vector3(Random.Range(vec.x-1,vec.x+1),Random.Range(vec.y-1,vec.y+1),0);
		return vec3;
	}
}
